//
//  SYSecondViewController.m
//  TermOfSectionB
//
//  Created by dlios on 15/12/2.
//  Copyright © 2015年 韩震明. All rights reserved.
//

#import "SYSecondViewController.h"
#import "SYSecondViewControllerModel.h"
#import "SYSecondMainTableViewCell.h"
#import "SYNextTableViewCell.h"
#import "SYNextTableViewModel.h"

@interface SYSecondViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *mainTableView;
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSMutableArray *mainArray;
@property (nonatomic, strong) NSMutableArray *nextArray;

@end

@implementation SYSecondViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self createMainTableView];
    
    [self createMainTableViewData];
    
    [self createNextTableViewData];
}

- (void)createMainTableView {
    self.mainTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    [self.view addSubview:self.mainTableView];
    self.mainTableView.dataSource = self;
    self.mainTableView.delegate = self;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 1;
    } else {
        return self.nextArray.count;
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return 0;
    } else {
        return 40;
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 50)];
    headerView.backgroundColor = [UIColor whiteColor];
    
    
    UILabel *middleLabel = [[UILabel alloc] initWithFrame:CGRectMake(130, 0, 100, 40)];
    middleLabel.backgroundColor = [UIColor whiteColor];
    middleLabel.text = @"精     选";
    middleLabel.textAlignment = NSTextAlignmentCenter;
    [headerView addSubview:middleLabel];
    
    
    UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(50, 20, 70, 1)];
    leftView.backgroundColor = [UIColor blackColor];
    [headerView addSubview:leftView];
    
    
    UIView *rightView = [[UIView alloc] initWithFrame:CGRectMake(240, 20, 70, 1)];
    rightView.backgroundColor = [UIColor blackColor];
    [headerView addSubview:rightView];
    
    return headerView;
}

- (void)viewWillAppear:(BOOL)animated {
    
    self.tabBarController.tabBar.hidden = YES;
    
}

- (void)viewWillDisappear:(BOOL)animated {
    self.tabBarController.tabBar.hidden = NO;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    
    if (indexPath.section == 0 && ![self.indexID isEqualToNumber:@7]) {
        self.mainTableView.rowHeight = 340;
        static NSString *reuse = @"reuse";
        SYSecondMainTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
        if (!cell) {
            cell = [[SYSecondMainTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuse];
        }
        
        cell.mainArray = self.mainArray;
        [cell.mainCollectionView reloadData];
        return cell;
        
    } else if (indexPath.section == 0 && [self.indexID isEqualToNumber:@7]) {
        self.mainTableView.rowHeight = 250;
        static NSString *reuse = @"reuse1";
        SYSecondMainTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
        if (!cell) {
            cell = [[SYSecondMainTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuse];
        }
        
        cell.mainArray = self.mainArray;
        [cell.mainCollectionView reloadData];
        return cell;
    
    } else {
        self.mainTableView.rowHeight = 180;
        static NSString *reuse = @"reuse2";
        SYNextTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
        if (!cell) {
            cell = [[SYNextTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuse];
        }
        SYNextTableViewModel *model = self.nextArray[indexPath.row];
        [cell.leftImage sd_setImageWithURL:[NSURL URLWithString:model.image]];
        cell.leftAddLabel.text = model.brand;
        cell.leftProductLabel.text = model.productDescription;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
}

// 解析第一个分区中的数据
- (void)createMainTableViewData {
    
    NSString *str = @"http://api.xiangqu.com/category/out/children";
    NSURL *url = [NSURL URLWithString:str];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request setHTTPMethod:@"post"];
    NSString *sqq = [NSString stringWithFormat:@"id=%@&key=5b745e19039468e23cc4f9e915a1805f&t=1445909497431&userid=705811", self.indexID];
    NSString *bodyStr = sqq;
    NSData *bodyData = [bodyStr dataUsingEncoding:NSUTF8StringEncoding];
    [request setHTTPBody:bodyData];
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
        
        self.mainArray = [NSMutableArray array];
        self.mainArray = [SYSecondViewControllerModel baseModelByArr:dic[@"data"]];
        
        [self.mainTableView reloadData];
    }];
    
}


// 解析第二个分区中的数据
- (void)createNextTableViewData {
    NSString *str = @"http://api.xiangqu.com/search/list";
    NSURL *url = [NSURL URLWithString:str];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request setHTTPMethod:@"post"];
    NSString *sqq = [NSString stringWithFormat:@"fromType=-1&imageHD=2&key=04f94d7ab62cfd07b33000cefa993cd4&outCategoryId=%@&page=0&t=1445914166026&userid=705811&version=3.5.4", self.indexID];
    NSString *bodyStr = sqq;
    NSData *bodyData = [bodyStr dataUsingEncoding:NSUTF8StringEncoding];
    [request setHTTPBody:bodyData];
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
        
        self.nextArray = [NSMutableArray array];
        self.nextArray = [SYNextTableViewModel baseModelByArr:dic[@"data"][@"records"]];
    
        [self.mainTableView reloadData];
    }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
